$(document).ready( function(){
	$(".removeFromWishlist").on("click", function(){
		var row = $(this).parents("tr");
		var tbody = $(this).parents("tbody");
		
		var productID = $(this).attr("product_id");
		
		var dataString = "&add_to_wishlist="+productID;
		$("#ajaxLoader").show();
		$.ajax({
				type:'POST',
				url: scriptsURL+"libs/ajax/ajax-includes.php",
				data: dataString,
				success: function(responseJSON)
				{
					response = JSON.parse(responseJSON);
					$("#ajaxLoader").hide();
					$("#messageDialog .modal-body").html("<p>"+response.message+"</p>");
					$('#messageDialog').modal('show');
					if(response.action == "delete")
					{
						row.fadeOut().remove();
						
						tbody.children("tr").each( function(index, element){
							$(this).children("td:eq(0)").html(index+1);
						});
						
					}
				}
		});
		return false;
	});
	
	$(document).on("click", ".addToWishlist", function(){
		var productID = $(this).attr("product_id");
		$("#ajaxLoader").show();
		
		var dataString = "&add_to_wishlist="+productID;
		var button = $(this);
		$.ajax({
				type:'POST',
				url: scriptsURL+"libs/ajax/ajax-includes.php",
				data: dataString,
				success: function(responseJSON)
				{
					response = JSON.parse(responseJSON);
					$("#ajaxLoader").hide();
					$("#messageDialog .modal-body").html("<p>"+response.message+"</p>");
					$('#messageDialog').modal('show');
					if(response.action == "delete")
					{
						if(button.children(".glyphicon").length > 0)
						{
							button.children(".glyphicon").removeClass("glyphicon-check").addClass("glyphicon-unchecked");
						}
						else
						{
							button.removeClass("active");
						}
					}
					else if(response.action == "add")
					{
						if(button.children(".glyphicon").length > 0)
						{
							button.children(".glyphicon").removeClass("glyphicon-unchecked").addClass("glyphicon-check");
						}
						else
						{
							button.addClass("active");
						}
					}
						
				}
		});
		return false;
	});
});
